/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.index.controller;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.l.core.util.WebUtils;
import com.l.core.web.RestCode;
import com.l.core.web.RestJson;
import com.l.web.master.index.entity.Password;
import com.l.web.master.index.entity.Profile;
import com.l.web.master.system.entity.SysUser;
import com.l.web.master.system.service.ISysUserService;

/**
 * <p>
 * BaseController 用户表 前端控制器
 * </p>
 *
 * @author 严秋旺
 * @since 2018-09-21
 */
@Controller
@RequestMapping("/master")
public class IndexController {
    @Autowired
    private ISysUserService userService;

    @RequestMapping({ "", "/", "index" })
    public String index(Model model) {
        return "master/index";
    }

    @RequestMapping("profile")
    public String profile(Model model) {
        SysUser user = userService.selectById(WebUtils.getLogin().getId());
        model.addAttribute("user", user);
        return "master/profile";
    }

    @RequestMapping("/saveProfile")
    @ResponseBody
    public RestJson saveProfile(@Valid Profile profile, Errors errors) {
        if (errors.hasErrors()) {
            return new RestJson(RestCode.FORM_BAD, errors);
        }

        SysUser user = new SysUser();
        BeanUtils.copyProperties(profile, user);
        user.setId(WebUtils.getLogin().getId());
//        userService.updateById(user);
        return RestJson.Success();
    }

    /**
     * 修改密码页面
     * 
     * @param model
     * @return
     */
    @RequestMapping("/password")
    public String pwd(Model model) {
        return "master/password";
    }

    /**
     * 修改密码
     */
    @RequestMapping("/savePassword")
    @ResponseBody
    public RestJson savePassword(@Valid Password password, Errors errors) {
        if (errors.hasErrors()) {
            return new RestJson(RestCode.FORM_BAD, errors);
        }

        SysUser user = userService.selectById(WebUtils.getLogin().getId());
        String encrypt = user.getPassword();
        user.setPassword(password.getOldPassword());
        String oldEncrypt = userService.encryptPassword(user);

        if (!oldEncrypt.equals(encrypt)) {
            return RestJson.Failed().setMessage("旧密码错误");
        }

        SysUser update = new SysUser();
        update.setId(user.getId());
        update.setPassword(password.getNewPassword());
        String newEncrypt = userService.encryptPassword(update);
        update.setPassword(newEncrypt);
        userService.updateById(update);
        return RestJson.Success();
    }

}